package com.pay.utils;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by Administrator on 2017-6-30.
 */
public class HttpUtils {
    private static PoolingHttpClientConnectionManager connectionManager;
    private static RequestConfig requestConfig;
    private static final int MAX_TIMEOUT=7000;

    static {
        //设置连接池
        connectionManager = new PoolingHttpClientConnectionManager();
        //设置连接池大小
        connectionManager.setMaxTotal(100);
        connectionManager.setDefaultMaxPerRoute(connectionManager.getMaxTotal());

        RequestConfig.Builder configBuilder = RequestConfig.custom();
        //设置连接超时
        configBuilder.setConnectTimeout(HttpUtils.MAX_TIMEOUT);
        //设置读取超时
        configBuilder.setSocketTimeout(HttpUtils.MAX_TIMEOUT);
        //设置从连接池获取连接实例的超时
        configBuilder.setConnectionRequestTimeout(HttpUtils.MAX_TIMEOUT);
        //提交请求前，测试连接是否可用
        configBuilder.setStaleConnectionCheckEnabled(true);

        requestConfig = configBuilder.build();
    }

    public static String doGet(String url){
        return doGet(url,new HashMap<String,Object>());
    }

    public static String doGet(String url,HashMap<String,Object> params){
        String apiUrl = url;
        StringBuffer param = new StringBuffer();
        int i = 0;
        for(String key : params.keySet()){
            if(i == 0){
                param.append("?");
            }else{
                param.append("&");
            }
            param.append(key).append("=").append(params.get(key));
            i++;
        }
        apiUrl += param;
        System.out.println("==============>"+apiUrl);
        String result="";
        DefaultHttpClient httpClient = new DefaultHttpClient();
        try {
            HttpPost httpGet = new HttpPost(apiUrl);
            HttpResponse httpResponse = httpClient.execute(httpGet);

            int returnCode = httpResponse.getStatusLine().getStatusCode();
            System.out.println("返回状态码："+returnCode);
            HttpEntity httpEntity = httpResponse.getEntity();
            if(null != httpEntity){
                InputStream inputStream = httpEntity.getContent();
                result = IOUtils.toString(inputStream,"UTF-8");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        HashMap<String,Object> params = new HashMap<String, Object>();
        params.put("q","SSLContexts");
        //params.put("orderNo",new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        //params.put("name","wql");
        System.out.println(doGet("http://mvnrepository.com/search",params));

    }

}
